﻿package mortal.game.view.copy.ChallengeCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import frEngine.*;
    import frEngine.event.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.ChallengeCopy.data.*;
    import mortal.game.view.copy.ChallengeCopy.newView.*;
    import mortal.game.view.copy.ChallengeCopy.view.*;
    import mortal.game.view.copy.copyFailWin.*;
    import mortal.game.view.shopMall.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ChallengeCopyController extends CopyControllerBase
    {
        private var _challengeModule:ChallengeCopyModule;

        public function ChallengeCopyController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._challengeModule == null)
            {
                this._challengeModule = new ChallengeCopyModule();
                this._challengeModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._challengeModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._challengeModule;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.ChallengeRewardInfo, this.showPassWin);
            NetDispatcher.addCmdListener(ServerCommand.ChalengePanelShow, this.showTimeWin);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.newDayUpdate);
            Dispatcher.addEventListener(EventName.ChallengeCopyOpen, this.openWin);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.guideInited);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.guideInited);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            GameProxy.copy.getPlayerChallengeCopyInfo();
            NetDispatcher.addCmdListener(ServerCommand.ChallengInfoUpdate, this.updateInfo);
            NetDispatcher.addCmdListener(ServerCommand.ChallengeUpdateRecord, this.updataRecord);
            NetDispatcher.addCmdListener(ServerCommand.ChallengeClearCDSuccess, this.updateLeftTime);
            NetDispatcher.addCmdListener(ServerCommand.ChallengMopSuccess, this.challengMopSuccess);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemsChange);
            Dispatcher.addEventListener(EventName.ChallengCopyEnter, this.enterChallengCopyHandler);
            Dispatcher.addEventListener(EventName.ChallengeCopyGetRecord, this.getRecord);
            Dispatcher.addEventListener(EventName.ChallengeClearCD, this.clearCD);
            Dispatcher.addEventListener(EventName.ChallengeMopSection, this.challengeMopTower);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateTab);
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            ShopFeaturTips.setIsUseGoldByType("challengeCopy", false);
            NetDispatcher.removeCmdListener(ServerCommand.ChallengeUpdateRecord, this.updataRecord);
            NetDispatcher.removeCmdListener(ServerCommand.ChallengInfoUpdate, this.updateInfo);
            NetDispatcher.removeCmdListener(ServerCommand.ChallengeClearCDSuccess, this.updateLeftTime);
            NetDispatcher.removeCmdListener(ServerCommand.ChallengMopSuccess, this.challengMopSuccess);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemsChange);
            Dispatcher.removeEventListener(EventName.ChallengCopyEnter, this.enterChallengCopyHandler);
            Dispatcher.removeEventListener(EventName.ChallengeCopyGetRecord, this.getRecord);
            Dispatcher.removeEventListener(EventName.ChallengeClearCD, this.clearCD);
            Dispatcher.removeEventListener(EventName.ChallengeMopSection, this.challengeMopTower);
            Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.updateTab);
            return;
        }// end function

        private function backPackItemsChange(param1:Object) : void
        {
            this._challengeModule.updateEquipChipNum();
            return;
        }// end function

        private function guideInited(param1 = null) : void
        {
            if (GameController.autoGuide.hasGuideChallengeCopy())
            {
                Dispatcher.removeEventListener(EventName.NavbarButtonUIRowInited, this.guideInited);
                if (ChallengeCopyIcon.instance.isHide)
                {
                    ChallengeCopyIcon.instance.show();
                }
            }
            return;
        }// end function

        override protected function addListeners() : void
        {
            return;
        }// end function

        override protected function removeListeners() : void
        {
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            if (this.isViewShow)
            {
                view.hide();
            }
            if (ChallengeMagicTowerWin.isViewShow)
            {
                ChallengeMagicTowerWin.instance.hide();
            }
            ChallengeTimePanl3.instance.show();
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            if (ChallengeTimePanl3.isViewShow)
            {
                ChallengeTimePanl3.instance.hide();
            }
            var _loc_1:* = Cache.instance.copy.challengCopy.getDataByTowerAndSection(1, 1);
            if (_loc_1.passLevel > 2 && !this.isViewShow)
            {
                this.view.show();
            }
            Cache.instance.copy.challengCopy.isInChallengeCopy = false;
            AIManager.cancelAll();
            return;
        }// end function

        private function enterChallengCopyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ChallengeData;
            CopyUtil.copyCheck(this.enterChallengeCopy, _loc_2);
            return;
        }// end function

        private function enterChallengeCopy(param1:ChallengeData) : void
        {
            if (Cache.instance.copy.challengCopy.residuettimes > 0)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35141), MsgRollTipsType.msgRollTips3);
            }
            if (param1.passLevel == param1.currentSectionMaxLevel)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35142), MsgRollTipsType.msgRollTips3);
            }
            if (param1.tChallengCopy.section <= 4 && param1.tChallengCopy.section > Cache.instance.copy.challengCopy.getHightestSectionByTower(param1.tChallengCopy.tower))
            {
                return MsgManager.showRollTipsMsg(Language.getString(35143), MsgRollTipsType.msgRollTips3);
            }
            if (Cache.instance.role.roleInfo.level < param1.tChallengCopy.towerLimitLv)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35144, param1.tChallengCopy.towerLimitLv), MsgRollTipsType.msgRollTips3);
            }
            GameProxy.copy.enterChallengeCopy(param1.tChallengCopy.tower, param1.tChallengCopy.section, (param1.passLevel + 1));
            return;
        }// end function

        private function updateInfo(param1:Object) : void
        {
            if (this.isViewShow)
            {
                this._challengeModule.changeToheightestLevel();
                this.updateLeftTime();
            }
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            if (GameController.autoGuide.hasGuideChallengeCopy())
            {
                if (this.isViewShow)
                {
                    view.hide();
                }
                else
                {
                    view.show();
                    if (event.data && event.data.hasOwnProperty("floor"))
                    {
                        this._challengeModule.setSelectLevelByTabAndIndex(event.data.floor, event.data.level);
                    }
                    else if (event.data != null)
                    {
                        this._challengeModule.setSelectLevelByTabAndIndex(1, 1);
                    }
                    else
                    {
                        this._challengeModule.changeToheightestLevel();
                    }
                }
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(35145));
            }
            return;
        }// end function

        private function showPassWinLattar() : void
        {
            return;
        }// end function

        private function showPassWin(param1:Object) : void
        {
            var _loc_2:* = param1 as SChallengeCopyRewards;
            ChallengeTimePanl3.instance.stopCount();
            if (_loc_2.result.isPass)
            {
                ChallengePassWin.instance.show();
                ChallengePassWin.instance.reward = _loc_2;
            }
            else
            {
                CopyFailBaseWin.instance.show();
                CopyFailBaseWin.instance.tipsImageUrl = ImagesConst.copyFailBaseWinTxt_2;
            }
            return;
        }// end function

        private function updateTab(event:Event) : void
        {
            if (this.isViewShow)
            {
                this._challengeModule.updateTowerList();
            }
            return;
        }// end function

        private function showTimeWin(param1:SChallengeCopyTotalTime) : void
        {
            var _loc_3:TChallengeCopy = null;
            var _loc_4:String = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:String = null;
            var _loc_8:EffectPlayer = null;
            ChallengeTimePanl3.instance.show();
            ChallengeTimePanl3.instance.data = param1;
            Cache.instance.copy.challengCopy.isInChallengeCopy = true;
            var _loc_2:* = ChallengeCopyConfig.instance.getMaxLevelByTowerAndSection(param1.floor, param1.level);
            if (param1.diff == _loc_2)
            {
                _loc_3 = ChallengeCopyConfig.instance.getDataByTowerAndSectionAndDif(param1.floor, param1.level, param1.diff);
                if (_loc_3)
                {
                    _loc_4 = _loc_3.preEffect;
                    if (_loc_4)
                    {
                        _loc_5 = _loc_4.split("#");
                        _loc_6 = 0;
                        while (_loc_6 < _loc_5.length)
                        {
                            
                            _loc_7 = _loc_5[_loc_6];
                            _loc_8 = EffectPlayerPool.instance.getEffectPlayer(_loc_7, null, null, true, true, false);
                            FrEventDispatcher.instance.proxyAddEventListener(_loc_8, Engine3dEventName.InitComplete, this.onEffectInitCompl);
                            _loc_6++;
                        }
                    }
                }
            }
            return;
        }// end function

        private function onEffectInitCompl(event:ParamsEvent) : void
        {
            if (event.currentTarget is EffectPlayer)
            {
                (event.currentTarget as EffectPlayer).dispose();
            }
            return;
        }// end function

        private function updataRecord(param1:Object) : void
        {
            this._challengeModule.updateRecord(param1 as SChallengeCopyRecords);
            return;
        }// end function

        private function getRecord(event:DataEvent) : void
        {
            GameProxy.copy.getPlayerChallengeRecord(event.data.floor, event.data.level, event.data.dif);
            return;
        }// end function

        private function clearCD(event:DataEvent) : void
        {
            var code:int;
            var cost:int;
            var closeAlert:Function;
            var e:* = event;
            closeAlert = function (param1:int) : void
            {
                var _loc_2:String = null;
                if (param1 == Alert.OK)
                {
                    _loc_2 = Language.getString(35147);
                    ShopFeaturTips.showUseGlodWin("challengeCopy", cost, _loc_2, null, GameProxy.copy.clearChallengeCopyCD, code, cost);
                }
                return;
            }// end function
            ;
            code = e.data.code;
            var leftTime:* = e.data.leftTime as int;
            cost = Math.ceil(leftTime / 120);
            var txt:* = Language.getStringByParam(35146, cost);
            Alert.isFlowMouse = true;
            Alert.show(txt, null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
            return;
        }// end function

        private function updateLeftTime(param1:Object = null) : void
        {
            this._challengeModule.updateLeftTime();
            return;
        }// end function

        private function challengeMopTower(event:DataEvent) : void
        {
            if (!Global.isDebugModle && ClockManager.instance.serverOpenDateNum == 1)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35148), MsgRollTipsType.msgRollTips3);
            }
            var _loc_2:* = event.data as TChallengeCopy;
            GameProxy.copy.mopChallengeCopy(_loc_2.tower, _loc_2.section);
            Cache.instance.copy.challengCopy.mopTower = _loc_2.tower;
            return;
        }// end function

        private function challengMopSuccess(param1:SChallengeMopRewardMsg) : void
        {
            this._challengeModule.updateMop();
            ChallengeMopResutlWin.instance.show();
            ChallengeMopResutlWin.instance.data = param1;
            return;
        }// end function

        private function newDayUpdate(param1:Object) : void
        {
            GameProxy.copy.getPlayerChallengeCopyInfo();
            return;
        }// end function

    }
}
